package de.gameduell.framework.gui.animation {
	import fl.transitions.TweenEvent;
	
	import de.gameduell.framework.debug.Debug;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.filters.GlowFilter;
	import flash.utils.Timer;	

	/**
	 * @author cbra
	 */
	public class GlowTween extends CombinedTween{
		private var glow:GlowFilter;
		private var displayObject:DisplayObject;

		public function GlowTween(aGlow:GlowFilter,targetGlow:GlowFilter,aDisplayObject:DisplayObject,easeType:Function,duration:Number,useSeconds:Boolean) {
//			new GlowFilter(0xFFFFFF,1,0,0,0.27,BitmapFilterQuality.MEDIUM, false, false);
			displayObject = aDisplayObject;
			glow = aGlow;
			if(glow.color != targetGlow.color){
				addTween(new GDTween(glow,"color",easeType,glow.color,targetGlow.color,duration,useSeconds));
			}	
			
			if(glow.blurX != targetGlow.blurX){
				addTween(new GDTween(glow,"blurX",easeType,glow.blurX,targetGlow.blurX,duration,useSeconds));
			}
			
			if(glow.blurY != targetGlow.blurY){
				addTween(new GDTween(glow,"blurY",easeType,glow.blurY,targetGlow.blurY,duration,useSeconds));
			}
			
			if(glow.alpha != targetGlow.alpha){
				addTween(new GDTween(glow,"alpha",easeType,glow.alpha,targetGlow.alpha,duration,useSeconds));
			}
			
			if(glow.strength != targetGlow.strength){
				Debug.trace("GlowTween strengthTween");
				addTween(new GDTween(glow,"strength",easeType,glow.strength,targetGlow.strength,duration,useSeconds));
			}
		}
		
		
		override public function start():void{
			super.start();
			displayObject.addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(event:Event):void {
//			Debug.trace("GlowTween strength "+glow.strength);
			displayObject.filters = [glow];
//			displayObject.filters = [glow];
			Debug.trace("GlowTween strength "+displayObject.filters[0].strength);
		}
		
		override protected function onMotionEvent(event:TweenEvent):void {
			if(event.type == TweenEvent.MOTION_FINISH) {
				_tweenCounter--;
			}
			if(_tweenCounter == 0) {
				_isPlaying = false;
				displayObject.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
				dispatchEvent(new TweenEvent(TweenEvent.MOTION_FINISH, event.time, event.position, event.bubbles, event.cancelable));
			}
		}
		
	}
}
